﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace DotNetX
{
    /// <summary>
    /// This class indicates what type a string is
    /// E.G. if a string value is "5", the IsInt32 method will return true
    /// </summary>
    public static class StringType
    {
        /// <summary>
        /// This returns true if the value of the passed string is a 32-bit integer
        /// </summary>
        /// <param name="value">The string to check</param>
        /// <returns>True is the value of the passed string is an 32-bit integer</returns>
        public static bool IsInt32(string value)
        {
            if (value == null)
                throw new ArgumentNullException("'value' is null.");
            
            // do we have a string?
            if (value.Length == 0)
                return false;

            // is the string and integer?
            try
            {
                // try and convert the string
                Convert.ToInt32(value);

                // the string is an integer
                return true;
            }
            catch (FormatException ex)
            {
                // the string is not an int32
                return false;
            }
            catch (OverflowException ex)
            {
                // the string is not an int32
                return false;
            }
        }

        /// <summary>
        /// This returns true if the value of the passed string is a 64-bit integer
        /// </summary>
        /// <param name="value">The string to check</param>
        /// <returns>True is the value of the passed string is an 64-bit integer</returns>
        public static bool IsInt64(string value)
        {
            if (value == null)
                throw new ArgumentNullException("'value' is null.");

            // do we have a string?
            if (value.Length == 0)
                return false;

            // is the string and integer?
            try
            {
                // try and convert the string
                Convert.ToInt64(value);

                // the string is an integer
                return true;
            }
            catch (FormatException ex)
            {
                // the string is not an int64
                return false;
            }
            catch (OverflowException ex)
            {
                // the string is not an int64
                return false;
            }
        }

        /// <summary>
        /// This returns true if the value of the passed string is a double
        /// </summary>
        /// <param name="value">The string to check</param>
        /// <returns>True is the value of the passed string is an double</returns>
        public static bool IsDouble(string value)
        {
            if (value == null)
                throw new ArgumentNullException("'value' is null.");

            // do we have a string?
            if (value.Length == 0)
                return false;

            // is the string and integer?
            try
            {
                // try and convert the string
                Convert.ToDouble(value);

                // the string is an integer
                return true;
            }
            catch (FormatException ex)
            {
                // the string is not an int64
                return false;
            }
            catch (OverflowException ex)
            {
                // the string is not an int64
                return false;
            }
        }

        /// <summary>
        /// This returns true if the passed value is an E-Mail
        /// </summary>
        /// <param name="value">The string to check</param>
        /// <returns>True if the passed string is a valid E-Mail</returns>
        public static bool IsEMailAddress(string value)
        {
            if (value == null)
                throw new ArgumentNullException("'value' is null.");

            // do we have a string?
            if (value.Length == 0)
                return false;

            // is the string an E-Mail?
            Regex regex = RegexCreator.CreateEMailRegex();
            return regex.IsMatch(value);
        }

        /// <summary>
        /// This returns true if the value of the passed string is a Guid
        /// </summary>
        /// <param name="value">The string to check</param>
        /// <returns>True is the value of the passed string is a Guid</returns>
        public static bool IsGuid(string value)
        {
            if (value == null)
                throw new ArgumentNullException("'value' is null.");

            // do we have a string?
            if (value.Length == 0)
                return false;

            // is the string a Guid?
            try
            {
                // try and convert the string
                new Guid(value);

                // the string is an integer
                return true;
            }
            catch (FormatException ex)
            {
                // the string is not an int32
                return false;
            }
            catch (OverflowException ex)
            {
                // the string is not an int32
                return false;
            }
        }
    }
}
